package com.wn.controller;

import cn.hutool.jwt.JWTUtil;
import com.wn.bean.dto.UserDto;
import com.wn.common.Result;
import com.wn.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @author yellow Docter
 * @date 2024-07-08
 * @desc 蜗牛创想：www.woniuxy.com
 */

@RestController
public class UserController {

    @Autowired
    private IUserService userService;

    @GetMapping("/hello")
    public String hello() {
        return "hello ";
    }

    @GetMapping("ok")
    public Result<String> ok() {
        return Result.ok();
    }

    @GetMapping("fail")
    public Result<String> fail() {
        return Result.fail();
    }

    @PostMapping("/login")
    public Result login(@RequestBody UserDto userDto) {

        boolean flag = userService.login(userDto.getUsername(), userDto.getPassword());
        if (flag){
            Map<String,Object> playLoad = new HashMap<>();
            playLoad.put("username",userDto.getUsername());
            //生成了token
            String token = JWTUtil.createToken(playLoad, "1234".getBytes());
            Map<String,Object> map = new HashMap<>();
            map.put("token",token);
            return Result.ok(map);
        }
        return  Result.fail("登录失败...");
    }

}
